--[[
工具类
@author 赵占涛
  ]]

local Util = {}
-- 创建一个uuid
function Util.uuid()
    local template ="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    return string.gsub(template, "x", function (c)
        local v = (c == "x") and math.random(0, 0xf) or math.random(8, 0xb)
        return string.format("%x", v)
    end)
end
-- 创建一个button
function Util.newButton(image, scale, pos, callback)
    local button = ccui.Button:create(image,image,image)
    button:setPosition(display.cx, display.cy)
    button:setEnabled(true)
    button:setScale(scale)
    button:setPosition(pos)
    button:addTouchEventListener(function(sender, eventType)
        print("Button click")
        if eventType == 0 then
            sender:setScale(scale * 1.1)
        elseif eventType == 2 then
            sender:setScale(scale)
            callback()
        elseif eventType == 3 then
            sender:setScale(scale)
        end
    end)
    return button
end

-- 显示一个对话框
-- @param 传入标题，两个按钮上的文字，以及两个回调
function Util.createDialog(title, buttonStr1, callback1, buttonStr2, callback2)
    if Util.dialogLayer then
        Util.dialogLayer:removeFromParent()
        Util.dialogLayer = nil
    end
    -- 对话框背景
    local dialogLayer = cc.LayerColor:create(ColorCfg.white)
    dialogLayer:setContentSize(display.width, display.height / 3)
    dialogLayer:setPosition(0, - display.height / 2)
    display.getRunningScene():addChild(dialogLayer, 1000)
    dialogLayer:runAction(cc.MoveTo:create(0.3, cc.p(0,200)))
    -- 标题
    local titleString = cc.Label:create()
    titleString:setTextColor(ColorCfg.purple)
    titleString:setString(title)
    titleString:setSystemFontSize(50)
    titleString:setAnchorPoint(0, 0.5)
    titleString:setPosition(50, dialogLayer:getContentSize().height - 50)
    dialogLayer:addChild(titleString, 10)
    print("outAction", outAction)

    local touchButton = Util.newButton(
        "nothingBtn.png",
        1,
        cc.p(dialogLayer:getContentSize().width/2, dialogLayer:getContentSize().height/2),
        function() print("点击了对话框的空白外，不处理") end
    )
    touchButton:setScale9Enabled(true)
    touchButton:setContentSize(dialogLayer:getContentSize())
    dialogLayer:addChild(touchButton, 0)
    --第一个按钮
    local button1 = Util.newButton(
        "commonBtn.png",
        1,
        cc.p(dialogLayer:getContentSize().width / 2, dialogLayer:getContentSize().height/ 2),
        function()
            print("点了按钮", dialogLayer, outAction)
            local outAction = cc.Sequence:create(
                cc.MoveTo:create(0.2, cc.p(0, - dialogLayer:getContentSize().height)),
                cc.CallFunc:create(function()
                    dialogLayer:removeFromParent()
                    Util.dialogLayer = nil
                    callback1()
                end)
            )
            dialogLayer:runAction(outAction)
        end)
    button1:setTitleText(buttonStr1)
    button1:setTitleFontSize(50)
    button1:setScale9Enabled(true)
    button1:setContentSize(dialogLayer:getContentSize().width * 0.8, 100)
    dialogLayer:addChild(button1, 10)
    --第2个按钮
    local button2 = Util.newButton(
        "commonBtn.png",
        1,
        cc.p(dialogLayer:getContentSize().width / 2, 100),
        function()
            print("点了按钮", dialogLayer, outAction)
            local outAction = cc.Sequence:create(
                cc.MoveTo:create(0.2, cc.p(0, - dialogLayer:getContentSize().height)),
                cc.CallFunc:create(function()
                    dialogLayer:removeFromParent()
                    Util.dialogLayer = nil
                    callback2()
                end)
            )
            dialogLayer:runAction(outAction)
        end)
    button2:setTitleText(buttonStr2)
    button2:setTitleFontSize(50)
    button2:setScale9Enabled(true)
    button2:setContentSize(dialogLayer:getContentSize().width * 0.8, 100)
    dialogLayer:addChild(button2, 10)

    Util.dialogLayer = dialogLayer
end

-- 播放音效
function Util.playEffect()
    if MUSIC_ENABLE then
        cc.SimpleAudioEngine:getInstance():playEffect("pop_star.mp3")
    end
end
return Util